package com.pktechan.api.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pktechan.api.entity.WxPayCloseOrder;
import com.pktechan.api.service.WxPayCloseOrderService;
import com.pktechan.api.utils.HtmlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description: 微信支付关闭订单功能
 * @author: s.zhang
 * @create: 2021-03-05 17:41
 **/
@RestController
@RequestMapping("wxPayCloseOrderController")
public class WxPayCloseOrderController {
    private static final Logger logger = LoggerFactory.getLogger(WxPayCloseOrderController.class);

    @Autowired
    private WxPayCloseOrderService wxPayCloseOrderService;

    /**
     * 根据id查询微信支付关闭订单
     *
     * @param closeOrderId
     * @param response
     */
    @RequestMapping(value = "getWxPayCloseOrderById", method = RequestMethod.POST)
    public void getWxPayCloseOrderById(@RequestParam Integer closeOrderId, HttpServletResponse response) {
        try {
            logger.info("根据id=" + closeOrderId + "查询购物车");
            WxPayCloseOrder wxPayCloseOrder = wxPayCloseOrderService.getById(closeOrderId);
            HtmlUtil.writerSuccessJson(response, wxPayCloseOrder);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 微信支付关闭订单列表
     *
     * @param wxPayCloseOrder
     * @param response
     */
    @RequestMapping(value = "getWxPayCloseOrderList", method = RequestMethod.POST)
    public void getWxPayCloseOrderList(@RequestBody WxPayCloseOrder wxPayCloseOrder, HttpServletResponse response) {
        try {
            logger.info("微信支付关闭订单列表");
            PageHelper.startPage(wxPayCloseOrder.getPageNo(), wxPayCloseOrder.getPageSize());
            List<WxPayCloseOrder> list = wxPayCloseOrderService.getList(wxPayCloseOrder);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 添加到微信支付关闭订单表
     *
     * @param wxPayCloseOrder
     * @param response
     */
    @RequestMapping(value = "saveWxPayCloseOrder", method = RequestMethod.POST)
    public void saveWxPayCloseOrder(@RequestBody WxPayCloseOrder wxPayCloseOrder, HttpServletResponse response) {
        try {
            logger.info("添加到微信支付关闭订单表");
            wxPayCloseOrderService.save(wxPayCloseOrder);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据Id修改微信支付关闭订单
     *
     * @param wxPayCloseOrder
     * @param response
     */
    @RequestMapping(value = "updateWxPayCloseOrderById", method = RequestMethod.POST)
    public void updateWxPayCloseOrderById(@RequestBody WxPayCloseOrder wxPayCloseOrder, HttpServletResponse response) {
        try {
            logger.info("根据id=" + wxPayCloseOrder.getId() + "修改微信支付关闭订单");
            wxPayCloseOrderService.update(wxPayCloseOrder);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

}
